// pages/sendneedorder/sendneedorder.js
var app=getApp();
var c=require('../common.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showPayPwdInput:false,  //是否展示密码输入层
    pwdVal: '',  //输入的密码
    payFocus: true, //文本框焦点
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var order_id= options.order_id;
    var order_num = options.order_num;
    var paystatys = options.paystatys;
    if (paystatys){
      this.setData({ showPayPwdInput:true})
    }
    this.setData({ order_id: order_id, order_num: order_num});
    var that=this;
    var demand_id=options.id;

    that.setData({
      demand_id: demand_id,
    })
    c.ajax(
      app.url +'describeselect',
      { demand_id: demand_id},
      res=>{
        console.log(res);
        that.setData({ content: res.data.content, price: res.data.price, status: res.data.status})
      }
    )
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  wangsendmoney:function(){
    var status = this.data.status;
    if (status==0){
        wx.showToast({
          title: '余额不足',
          icon:'none'
        })
        return;
    }
  },
  /**
  * 显示支付密码输入层
  */
  showInputLayer: function () {
    var status = this.data.status;
    var order_id = this.data.order_id;
    var id = this.data.demand_id;
    var order_num = this.data.order_num;
    var user_id = wx.getStorageSync("userInfo").user_id;
    var money = this.data.price;
    c.ajax(
      app.url + 'getUpass', {
        user_id: user_id
      },
      res => {
        var status = res.data;
        if (status == 101) {
          wx.showToast({
            title: '请先设置支付密码',
            icon: 'none',
          })
          setTimeout(function () {
            wx.navigateTo({
              url: '/pages/sendneedsetpass2/sendneedsetpass2?type=1&id=' + id + '&order_id=' + order_id + '&order_num=' + order_num,
            })
          }, 1000)
          return;
        } else {
          c.ajax(
            app.url + 'myinfo', {
              user_id: user_id
            },
            res => {

              var mymoney = parseInt(res.data.money);
              if (mymoney < money) {
                wx.showToast({
                  title: '约课币不足，请充值',
                  icon: 'none',
                  image: '',
                  duration: 2000,
                  mask: true,
                  success: function (res) { },
                  fail: function (res) { },
                  complete: function (res) { },
                })
                setTimeout(function () {
                  wx.navigateTo({
                    url: '/pages/myzhanghu/myzhanghu',
                  })
                }, 2000)
                return;
              } else {
                this.setData({
                  showPayPwdInput: true
                })
              }
            }
          )
        }
      }
    )
    // if (status==0){
    //  wx.showToast({
    //    title: '余额不足',
    //    icon:'none'
    //  })
    //  return;
    // }
    // this.setData({ showPayPwdInput: true, payFocus: true });
  },
  /**
   * 隐藏支付密码输入层
   */
  hidePayLayer: function () {
    var val = this.data.pwdVal;
    var order_id = this.data.order_id;
    var uid=app.u.user_id;
    var that=this;
    this.setData({ showPayPwdInput: false, payFocus: false, pwdVal: '' }, function () {
      var demand_id = that.data.demand_id;
      c.ajax(
        app.url +'orderpay',
        { uid: uid, val: val, order_id: order_id, demand_id: demand_id},
          res=>{
            console.log(res.data);
            if(res.data){
              wx.navigateTo({
                url: '/pages/sendneedsuccess/sendneedsuccess',
              })
            }else{
              wx.showToast({
                title: '支付密码错误',
                icon:'none'
              })
            }
          }
      )
    });

  },
  /**
   * 获取焦点
   */
  getFocus: function () {
    this.setData({ payFocus: true });
  },
  /**
   * 输入密码监听
   */
  inputPwd: function (e) {
    this.setData({ pwdVal: e.detail.value });

    if (e.detail.value.length >= 6) {
      this.hidePayLayer();
    }
  },
  passwordset:function(){
    var order_num = this.data.order_num;
    var order_id = this.data.order_id;
    var id = this.data.demand_id;
     wx.navigateTo({
       url: '/pages/sendneedsetpass/sendneedsetpass?type=1' + '&order_id=' + order_id + '&order_num=' + order_num+'&id='+id,
     })
  },
  kefuclick: function () {

    wx.showModal({
      title: '',
      content: '4000063088',
      confirmText: '拨打',
      success(res) {
        if (res.confirm) {
          wx.makePhoneCall({
            phoneNumber: '4000063088',
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }
})